#include "../server.hpp"

class EchoServer
{
    using PtrConnection = std::shared_ptr<Connection>;
public:
    EchoServer(uint16_t port=8888):_svr(port)
    {
        _svr.SetThreadPoolSize(2);
        _svr.SetInactiveRelease(10);
        _svr.SetClosedCallBack(std::bind(&EchoServer::OnClose, this, std::placeholders::_1));
        _svr.SetConnectedCallBack(std::bind(&EchoServer::OnConnected, this, std::placeholders::_1));
        _svr.SetMessageCallBack(std::bind(&EchoServer::OnMessage, this, std::placeholders::_1, std::placeholders::_2));
    }

    std::unordered_map<uint64_t,PtrConnection> cnns;
    void OnConnected(const PtrConnection& cnn) {lg(Debug,"New Connection: %p",cnn.get());}
    void OnClose(const PtrConnection& cnn) {lg(Debug,"Close Connection: %p",cnn.get());}
    void OnMessage(const PtrConnection& cnn,Buffer* bf)
    {
        cnn->Send(bf->ReadPos(),bf->ReadableSize()); 
        bf->MoveReadOffset(bf->ReadableSize());
        cnn->Shutdown();
    }
    void Start() {_svr.Start();}

private:
    TcpServer _svr;
};